package MemoryManager;

import java.util.Arrays;

public class Process {
    private int PID; // 进程标识符
    private int processState; // 进程状态
    private int cpuTime; // 进程已用 CPU 时间
    private int startTime; // 进程创建时间
    public int needTimeforDevice;  // 设备占用剩余时间
    public char currentDeviceName;  // 占用设备的名称
    private String[] commands; // 储存命令的字符串数组
    public int commandP; // 命令数组指针
    public static String COMMAND_SEPARATOR = ";";  // 命令分隔符
    public static int STATE_BLOCK = 0; // 阻塞态
    public static int STATE_READY = 1; // 就绪态
    public static int STATE_FINISH = 2; // 完成态
    public int A; // A资源需求
    public int B; // B资源需求
    public int C; // C资源需求
    public boolean runned;  // 是否已运行

    // 构造方法
    public Process(String fileContent, int PID, int startTime) {
        this.PID = PID;
        // 解析命令
        contentAnalysis(fileContent);
        cpuTime = 0;
        commandP = 0;
        processState = STATE_READY;
        this.startTime = startTime;
    }
    
    // 解析命令
    public void contentAnalysis(String fileContent) {
        commands = fileContent.split(COMMAND_SEPARATOR);
        A = Integer.parseInt(commands[0].substring(2));
        B = Integer.parseInt(commands[1].substring(2));
        C = Integer.parseInt(commands[2].substring(2));
        if (commands[commands.length - 1].getBytes()[0] == 0)
            commands = Arrays.copyOf(commands, commands.length - 1);
        commands = Arrays.copyOfRange(commands, 3, commands.length);
    }
    
 
    public int getA() {
        return A;
    }

    public int getB() {
        return B;
    }

    public int getC() {
        return C;
    }

    // 获取进程标识符
    public int getPID() {
        return PID;
    }

    // 设置进程标识符
    public void setPID(int pid) {
        this.PID = pid;
    }

    // 获取进程状态
    public int getProcessState() {
        return processState;
    }

    // 设置进程状态
    public void setProcessState(int processState) {
        this.processState = processState;
    }

    // 获取进程已用 CPU 时间
    public long getCpuTime() {
        return cpuTime;
    }

    // 设置进程已用 CPU 时间
    public void setCpuTime(int cpuTime) {
        this.cpuTime = cpuTime;
    }

    // 获取进程创建时间
    public int getStartTime() {
        return startTime;
    }

    // 设置进程创建时间
    public void setStartTime(int creationTime) {
        this.startTime = creationTime;
    }

    //获取命令
    public String[] getCommands() {
        return commands;
    }

    //设置命令数组
    public void setCommands(String[] commands) {
        this.commands = commands;
    }

    // 运行进程，完成返回-1，否则返回PID
    public int run() {
        if (runned)
            return PID;
        runned = true;
        String curCommand = commands[commandP];
        if (curCommand.equals("END")) {
            // 进程完成
            setProcessState(STATE_FINISH);
            return -1;
        } else if (processState == STATE_BLOCK) {
            // 进程阻塞
            if (--needTimeforDevice == 0) {
                ++commandP;
                processState = STATE_READY;
            }
        } else if (curCommand.charAt(0) == '!') {
            // 进程需要使用设备
            processState = STATE_BLOCK;
            currentDeviceName = curCommand.charAt(1);
            needTimeforDevice = Integer.parseInt(curCommand.substring(2));
            return -1;
        } else {
            // 进程运行
            ++cpuTime;
            ++commandP;
        }
        return PID;
    }

    public void SS() {
    }
}
